home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / gfx / misc / SafeClip.lha / SafeClip / GCC / safeclip.c < prev    next >
C/C++ Source or Header  |  1996-03-29  |  5KB  |  236 lines

  1. /****h* Autodoc/safeclip.c [1.0] *
  2. *  NAME
  3. *    safeclip.c
  4. *  FUNCTION
  5. *    Interface to system rendering routines, but with clipping.
  6. *  AUTHOR
  7. *    Peter Knight: All programming.
  8. *    <pak@star.sr.bham.ac.uk>
  9. *  CREATION DATE
  10. *    24-Mar-96
  11. *  COPYRIGHT
  12. *    No restriction (Public Domain).
  13. *    Use these routines as you wish in your programs (a 
  14. *    mention in the credits would be nice, but it's up to
  15. *    you).
  16. **********
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <proto/exec.h>
  22. #include <proto/graphics.h>
  23.  
  24. #include "safeclip.h"
  25.  
  26. /* Global variables. */
  27. LONG CLP_xmin, CLP_ymin, CLP_xmax, CLP_ymax;
  28. LONG (*CLP_vert)[2] = NULL, (*CLP_wrk)[2] = NULL;
  29. LONG CLP_nvert = 0, CLP_nvertmax;
  30. LONG CLP_lastx, CLP_lasty;
  31.  
  32. /****i* safeclip.c/Clip2d
  33. * NAME
  34. *   Clip2d
  35. * SYNOPSIS
  36. *   res = Clip2d (n)
  37. *   LONG Clip2d (LONG)
  38. * FUNCTION
  39. *   C interface to ASM routine for clipping filled polygons.
  40. * INPUTS
  41. *   n - Number of vertices in polygon
  42. * RESULTS
  43. *   res - Number of vertices in clipped polygon
  44. * SEE ALSO
  45. */
  46. static __inline LONG
  47. Clip2d (LONG n)
  48. {
  49.   register LONG _res __asm("d0");
  50.   register LONG *a0 __asm("a0") = &CLP_vert[0][0];
  51.   register LONG *a1 __asm("a1") = CLP_wrk[0];
  52.   register LONG d0 __asm("d0") = n;
  53.   __asm ("jsr _clip2d"
  54.      : "=r" (_res)
  55.      : "r" (a0), "r" (a1), "r" (d0)
  56.      : "d0", "memory");
  57.   return _res;
  58. }
  59. /*********/
  60.  
  61. /****i* safeclip.c/ClipLine
  62. * NAME
  63. *   ClipLine
  64. * SYNOPSIS
  65. *   res = ClipLine ()
  66. *   LONG ClipLine (VOID)
  67. * FUNCTION
  68. *   C interface to ASM routine for clipping line segments.
  69. * INPUTS
  70. * RESULTS
  71. *   res - TRUE if any part of line was drawn, otherwise FALSE.
  72. * SEE ALSO
  73. */
  74. static __inline LONG
  75. ClipLine (VOID)
  76. {
  77.   register LONG _res __asm("d0");
  78.   register LONG *a0 __asm("a0") = &CLP_vert[0][0];
  79.   __asm ("jsr _lineclip"
  80.      : "=r" (_res)
  81.      : "r" (a0)
  82.      : "d0", "memory");
  83.   return _res;
  84. }
  85. /*********/
  86.  
  87. /****** safeclip.c/SafeAreaEnd
  88. * NAME
  89. *   SafeAreaEnd
  90. * SYNOPSIS
  91. *        SafeAreaEnd (rp)
  92. *   VOID SafeAreaEnd (struct RastPort *)
  93. * FUNCTION
  94. *   Process buffer AreaDraw() instructions
  95. * INPUTS
  96. *   rp - Pointer to RastPort on which to draw.
  97. * RESULTS
  98. * SEE ALSO
  99. *   SafeAreaDraw
  100. */
  101. VOID
  102. SafeAreaEnd (struct RastPort *rp)
  103. {
  104.   if (CLP_nvert)
  105.     {
  106.       WORD i;
  107.       SafeAreaDraw (CLP_vert[0][0], CLP_vert[0][1]);  /* ensure shape is closed */
  108.       CLP_nvert = Clip2d (CLP_nvert);
  109.       AreaMove (rp, CLP_vert[0][0], CLP_vert[0][1]);
  110.       for (i = 1; i < CLP_nvert; i++)
  111.     AreaDraw (rp, CLP_vert[i][0], CLP_vert[i][1]);
  112.       AreaEnd (rp);
  113.       CLP_nvert = 0;
  114.     }
  115. }
  116. /*********/
  117.  
  118. /****** safeclip.c/SafeRectFill
  119. * NAME
  120. *   SafeRectFill
  121. * SYNOPSIS
  122. *        SafeRectFill (rp, x1, y1, x2, y2)
  123. *   VOID SafeRectFill (struct RastPort *, LONG, LONG, LONG, LONG)
  124. * FUNCTION
  125. *   Draw a filled rectangle
  126. * INPUTS
  127. *   rp - Pointer to RastPort on which to draw.
  128. *   (x1,y1) - Coordinates of upper left corner
  129. *   (x2,y2) - Coordinates of lower right corner
  130. * RESULTS
  131. * SEE ALSO
  132. */
  133. VOID
  134. SafeRectFill (struct RastPort *rp, LONG x1, LONG y1, LONG x2, LONG y2)
  135. {
  136.   SafeAreaDraw (x1, y1);
  137.   SafeAreaDraw (x2, y1);
  138.   SafeAreaDraw (x2, y2);
  139.   SafeAreaDraw (x1, y2);
  140.   SafeAreaEnd (rp);
  141. }
  142. /*********/
  143.  
  144. /****** safeclip.c/SafeDraw
  145. * NAME
  146. *   SafeDraw
  147. * SYNOPSIS
  148. *        SafeDraw (rp, x, y)
  149. *   VOID SafeDraw (struct RastPort *, LONG, LONG)
  150. * FUNCTION
  151. *   Move from previous point to new point and draw line.
  152. * INPUTS
  153. *   rp - Pointer to RastPort on which to draw.
  154. *   (x,y) - Coordinates of point to draw to
  155. * RESULTS
  156. * SEE ALSO
  157. *   SafeMove
  158. */
  159. VOID
  160. SafeDraw (struct RastPort *rp, LONG x, LONG y)
  161. {
  162.   CLP_vert[0][0] = CLP_lastx;
  163.   CLP_vert[0][1] = CLP_lasty;
  164.   CLP_vert[1][0] = x;
  165.   CLP_vert[1][1] = y;
  166.   if (ClipLine ())
  167.     {
  168.       Move (rp, CLP_vert[0][0], CLP_vert[0][1]);
  169.       Draw (rp, CLP_vert[1][0], CLP_vert[1][1]);
  170.     }
  171.   CLP_lastx = x;
  172.   CLP_lasty = y;
  173. }
  174. /*********/
  175.  
  176. /****** safeclip.c/SafeInit
  177. * NAME
  178. *   SafeInit
  179. * SYNOPSIS
  180. *   res = SafeInit (nvertmax)
  181. *   ULONG SafeInit (ULONG)
  182. * FUNCTION
  183. *   Initialise clipping routines
  184. * INPUTS
  185. *   nvertmax - Maximum number of vertices per polygon that you will use
  186. * RESULTS
  187. *   res - FALSE if everything was OK, otherwise TRUE
  188. * SEE ALSO
  189. *   SafeClose
  190. */
  191. ULONG
  192. SafeInit (ULONG nvertmax)
  193. {
  194.   CLP_nvertmax = nvertmax;
  195.   if (!(CLP_vert = (LONG (*)[2]) AllocVec (8 * CLP_nvertmax, MEMF_PUBLIC)))
  196.     return 1;
  197.   if (!(CLP_wrk = (LONG (*)[2]) AllocVec (8 * CLP_nvertmax, MEMF_PUBLIC)))
  198.     {
  199.       FreeVec (CLP_vert);
  200.       CLP_vert = 0;
  201.       return 2;
  202.     }
  203.   return 0;
  204. }
  205. /*********/
  206.  
  207. /****** safeclip.c/SafeClose
  208. * NAME
  209. *   SafeClose
  210. * SYNOPSIS
  211. *        SafeClose ()
  212. *   VOID SafeClose (VOID)
  213. * FUNCTION
  214. *   Free memory allocated by SafeInit()
  215. * INPUTS
  216. * RESULTS
  217. * SEE ALSO
  218. *   SafeInit
  219. */
  220. VOID
  221. SafeClose (VOID)
  222. {
  223.   if (CLP_wrk)
  224.     {
  225.       FreeVec (CLP_wrk);
  226.       CLP_wrk = 0;
  227.     }
  228.   if (CLP_vert)
  229.     {
  230.       FreeVec (CLP_vert);
  231.       CLP_vert = 0;
  232.     }
  233. }
  234. /*********/
  235.  
  236.